package com.example.a_java.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.a_java.common.Result;
import com.example.a_java.entity.SysConfig;
import com.example.a_java.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Tag(name = "参数配置", description = "参数配置相关接口")
@RestController
@RequestMapping("/system/config")
@SecurityRequirement(name = "bearer-key")
public class SysConfigController {

    @Autowired
    private SysConfigService configService;

    @Operation(summary = "获取参数配置列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:config:list')")
    public Result<Page<SysConfig>> list(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "参数名称") @RequestParam(required = false) String configName,
            @Parameter(description = "参数键名") @RequestParam(required = false) String configKey,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        
        Page<SysConfig> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<SysConfig>()
                .like(StringUtils.hasText(configName), SysConfig::getConfigName, configName)
                .like(StringUtils.hasText(configKey), SysConfig::getConfigKey, configKey)
                .eq(status != null, SysConfig::getStatus, status)
                .orderByAsc(SysConfig::getConfigId);
        
        configService.page(page, wrapper);
        return Result.success(page);
    }

    @Operation(summary = "获取参数配置详情")
    @GetMapping("/{configId}")
    @PreAuthorize("hasAuthority('system:config:query')")
    public Result<SysConfig> getInfo(@PathVariable Long configId) {
        return Result.success(configService.getById(configId));
    }

    @Operation(summary = "根据参数键名查询参数值")
    @GetMapping("/key/{configKey}")
    public Result<String> getConfigValue(@PathVariable String configKey) {
        return Result.success(configService.getConfigValueByKey(configKey));
    }

    @Operation(summary = "新增参数配置")
    @PostMapping
    @PreAuthorize("hasAuthority('system:config:add')")
    public Result<Boolean> add(@RequestBody @Valid SysConfig config) {
        return Result.success(configService.save(config));
    }

    @Operation(summary = "修改参数配置")
    @PutMapping
    @PreAuthorize("hasAuthority('system:config:edit')")
    public Result<Boolean> update(@RequestBody @Valid SysConfig config) {
        return Result.success(configService.updateById(config));
    }

    @Operation(summary = "删除参数配置")
    @DeleteMapping("/{configId}")
    @PreAuthorize("hasAuthority('system:config:delete')")
    public Result<Boolean> delete(@PathVariable Long configId) {
        return Result.success(configService.removeById(configId));
    }
} 
